package com.lwk.demo3;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    // Used to load the 'native-lib' library on application startup.
    static {
        System.loadLibrary("native-lib");
    }
    Button bt1;
    Button bt2;
    Button bt3;
    Button bt4;
    Button bt5;
    Button bt6;
    Button bt7;
    Button bt8;
    Button bt9;
    Button bt10;
    Button bt11;
    Button bt12;

    TextView text;
    EditText input;
    int len ;
    String result = null;
    String originaltext = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        input = findViewById(R.id.input);
        text = findViewById(R.id.textView);
        //AES加密按钮
        bt1 = findViewById(R.id.jia);
        bt1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               String indata = input.getText().toString();
               len = indata.length();
                if (!input.getText().toString().equals("")) {
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    Aesencrypt(indata.getBytes(), outdata, outlen,true);
                    result = Convection.Bytes2HexString(outdata);
                    result = result.replaceAll("(0)+$", "");
                    System.out.println("AES加密后："+result);
                    text.setText("AES加密结果：" + result);
                }else {
                    text.setText("请输入正确内容！");
                    input.setText("");
                }
            }
        });
        //AES解密按钮
        bt2 = findViewById(R.id.jie);
        bt2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                System.out.println("加密后："+result);
                if (result != null){
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    Aesencrypt(Convection.hexString2Bytes(result), outdata, outlen, false);
                    System.out.println("加密后："+outdata);
                    String resultdecrypt = Convection.Bytes2HexString(outdata);
                    System.out.println("解密结果:"+resultdecrypt);
                    resultdecrypt = Convection.hexToString(resultdecrypt);
                    System.out.println("解密结果:"+resultdecrypt);
                    int resultlen = resultdecrypt.length();
                    if (resultlen > len){
                        resultdecrypt = resultdecrypt.substring(0,len);
                    }
                    text.setText("ASE解密结果："+resultdecrypt);
                }else {
                    text.setText("输入为空！");
                }
            }
        });



        //SM2加密按钮
        bt3 = findViewById(R.id.sm2encrypty);
        bt3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sm2encrypt = input.getText().toString();

                if (!input.getText().toString().equals("")) {
                    System.out.println("输入内容："+sm2encrypt);
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    System.out.println("输入的数据："+sm2encrypt.getBytes());
                    Sm2encrypt(sm2encrypt.getBytes(), outdata, outlen,true);
                    result = Convection.Bytes2HexString(outdata, outlen[0]);
                    System.out.println("SM2加密后："+result);
                    text.setText("SM2加密结果：" + result);
                }else {
                    text.setText("请输入正确内容！");
                    input.setText("");
                }
            }
        });

        //SM2解密按钮
        bt4 = findViewById(R.id.sm2decrypty);
        bt4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (result != null){
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    Sm2encrypt(Convection.hexString2Bytes(result), outdata, outlen, false);
                    System.out.println("加密后："+outdata);
                    String resultdecrypt = Convection.Bytes2HexString(outdata);
                    System.out.println("解密结果:"+resultdecrypt);
                    resultdecrypt = Convection.hexToString(resultdecrypt);
                    System.out.println("解密结果:"+resultdecrypt);
                    text.setText("ASE解密结果："+resultdecrypt);
                }else {
                    text.setText("输入为空！");
                }
            }
        });


        //RSA加密按钮
        bt5 = findViewById(R.id.rsaencrypty);
        bt5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String rsaencrypt = input.getText().toString();
                len = rsaencrypt.length();
                if (!input.getText().toString().equals("")) {
                    System.out.println("输入内容："+rsaencrypt);
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    System.out.println("输入的数据："+rsaencrypt.getBytes());
                    Rsaencrypt(rsaencrypt.getBytes(), outdata, outlen,true);
                    result = Convection.Bytes2HexString(outdata, outlen[0]);
                    System.out.println("SM2加密后："+result);
                    text.setText("SM2加密结果：" + result);
                }else {
                    text.setText("请输入正确内容！");
                    input.setText("");
                }
            }
        });

        //RSA解密按钮
        bt6 = findViewById(R.id.rsadecrypty);
        bt6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (result != null){
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    Rsaencrypt(Convection.hexString2Bytes(result), outdata, outlen, false);
                    System.out.println("加密后："+outdata);
                    String resultdecrypt = Convection.Bytes2HexString(outdata);
                    System.out.println("解密结果:"+resultdecrypt);
                    resultdecrypt = Convection.hexToString(resultdecrypt);
                    System.out.println("解密结果:"+resultdecrypt);
                    int resultlen = resultdecrypt.length();
                    if (resultlen > len){
                        resultdecrypt = resultdecrypt.substring(0,len);
                    }
                    text.setText("ASE解密结果："+resultdecrypt);
                }else {
                    text.setText("输入为空！");
                }
            }
        });


        //DES加密按钮
        bt7 = findViewById(R.id.desencrypty);
        bt7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String desencrypt = input.getText().toString();
                len = desencrypt.length();
                if (!input.getText().toString().equals("")) {
                    System.out.println("输入内容："+desencrypt);
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    System.out.println("输入的数据："+desencrypt.getBytes());
                    Desencrypt(desencrypt.getBytes(), outdata, outlen,true);
                    result = Convection.Bytes2HexString(outdata, outlen[0]);
                    System.out.println("SM2加密后："+result);
                    result = result.replaceAll("(0)+$", "");
                    text.setText("SM2加密结果：" + result);
                }else {
                    text.setText("请输入正确内容！");
                    input.setText("");
                }
            }
        });

        //DES解密按钮
        bt8 = findViewById(R.id.desdecrypty);
        bt8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (result != null){
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    Desencrypt(Convection.hexString2Bytes(result), outdata, outlen, false);
                    System.out.println("加密后："+outdata);
                    String resultdecrypt = Convection.Bytes2HexString(outdata);
                    System.out.println("解密结果:"+resultdecrypt);
                    resultdecrypt = Convection.hexToString(resultdecrypt);
                    System.out.println("解密结果:"+resultdecrypt);
                    int resultlen = resultdecrypt.length();
                    if (resultlen > len){
                        resultdecrypt = resultdecrypt.substring(0,len);
                    }
                    text.setText("ASE解密结果："+resultdecrypt);
                }else {
                    text.setText("输入为空！");
                }
            }
        });


        //DES3加密按钮
        bt9 = findViewById(R.id.des3encrypty);
        bt9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String des3encrypt = input.getText().toString();
                len = des3encrypt.length();
                if (!input.getText().toString().equals("")) {
                    System.out.println("输入内容："+des3encrypt);
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    System.out.println("输入的数据："+des3encrypt.getBytes());
                    Des3encrypt(des3encrypt.getBytes(), outdata, outlen,true);
                    result = Convection.Bytes2HexString(outdata, outlen[0]);
                    System.out.println("SM2加密后："+result);
                    result = result.replaceAll("(0)+$", "");
                    text.setText("SM2加密结果：" + result);
                }else {
                    text.setText("请输入正确内容！");
                    input.setText("");
                }
            }
        });

        //DES3解密按钮
        bt10 = findViewById(R.id.des3decrypty);
        bt10.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (result != null){
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    Des3encrypt(Convection.hexString2Bytes(result), outdata, outlen, false);
                    System.out.println("加密后："+outdata);
                    String resultdecrypt = Convection.Bytes2HexString(outdata);
                    System.out.println("解密结果:"+resultdecrypt);
                    resultdecrypt = Convection.hexToString(resultdecrypt);
                    System.out.println("解密结果:"+resultdecrypt);
                    int resultlen = resultdecrypt.length();
                    if (resultlen > len){
                        resultdecrypt = resultdecrypt.substring(0,len);
                    }
                    text.setText("ASE解密结果："+resultdecrypt);
                }else {
                    text.setText("输入为空！");
                }
            }
        });

        //Rsa签名按钮
        bt11 = findViewById(R.id.rsa1encrypty);
        bt11.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String rsaencrypt = input.getText().toString();
                originaltext = rsaencrypt;
                len = rsaencrypt.length();
                if (!input.getText().toString().equals("")) {
                    System.out.println("输入内容："+rsaencrypt);
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    System.out.println("输入的数据："+rsaencrypt.getBytes());
                    int flag = RsaSign(rsaencrypt.getBytes(), outdata, outlen);
                    result = Convection.Bytes2HexString(outdata, outlen[0]);
                    String resultsign = result;
//                    System.out.println("SM2加密后："+result);
                    System.out.println("验签成功与否"+flag);
                    text.setText("RSA签名结果：" + resultsign);
                    if (flag == 0){
                        Toast.makeText(MainActivity.this,"验签成功！",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(MainActivity.this,"验签失败！",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    text.setText("请输入正确内容！");
                    input.setText("");
                }
            }
        });

        //Sm2签名按钮
        bt12 = findViewById(R.id.sm2sign);
        bt12.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String sm2sign = input.getText().toString();
                originaltext = sm2sign;
                len = sm2sign.length();
                if (!input.getText().toString().equals("")) {
                    System.out.println("输入内容："+sm2sign);
                    byte[] outdata = new byte[4096];
                    int[] outlen = new int[1];
                    System.out.println("输入的数据："+sm2sign.getBytes());
                    int flag = Sm2sign(sm2sign.getBytes(), outdata, outlen);
                    System.out.println("SM2签名后未转换："+outdata);
                    result = Convection.Bytes2HexString(outdata, outlen[0]);
                    String resultsign = result;
                    System.out.println("SM2签名后："+result);
                    System.out.println("验签成功与否"+flag);
                    text.setText("RSA签名结果：" + resultsign);
                    if (flag == 0){
                        Toast.makeText(MainActivity.this,"验签成功！",Toast.LENGTH_SHORT).show();
                    }else {
                        Toast.makeText(MainActivity.this,"验签失败！",Toast.LENGTH_SHORT).show();
                    }
                }else {
                    text.setText("请输入正确内容！");
                    input.setText("");
                }
            }
        });

    }

    //关于解决加密结果乱码问题的方法
    public static String Bytes2HexString(byte[] b){
        String ret = "";
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            ret += hex.toUpperCase();
        }
        return ret;
    }
    public native void Aesencrypt(byte[] indata, byte[] outdata, int[] outlen, boolean flag);

    public native void Sm2encrypt(byte[] indata, byte[] outdata, int[] outlen, boolean flag);

    public native int Sm2sign(byte[] indata, byte[] outdata, int[] outlen);

    public native void Rsaencrypt(byte[] indata, byte[] outdata, int[] outlen, boolean flag);

    public native void Desencrypt(byte[] indata, byte[] outdata, int[] outlen, boolean flag);

    public native void Des3encrypt(byte[] indata, byte[] outdata, int[] outlen, boolean flag);

    public native int RsaSign(byte[] indata, byte[] outdata, int[] outlen);
}
